package com.demon.framework.boot.autoconfigure.scheduler;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;

/**
 * Quartz  SchedulerListener 监听器  日志记录
 *
 * @author Demon-HY
 * @date 2020-7-24
 */
@Slf4j
class LogSchedulerListener implements SchedulerListener {

    @Override
    public void jobScheduled(Trigger trigger) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void jobUnscheduled(TriggerKey triggerKey) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void triggerFinalized(Trigger trigger) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void triggerPaused(TriggerKey triggerKey) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void triggersPaused(String triggerGroup) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void triggerResumed(TriggerKey triggerKey) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void triggersResumed(String triggerGroup) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void jobAdded(JobDetail jobDetail) {
        log.info("监听到新增任务,{}", jobDetail);
    }

    @Override
    public void jobDeleted(JobKey jobKey) {
        log.info("监听到删除任务,{}", jobKey);
    }

    @Override
    public void jobPaused(JobKey jobKey) {
        log.info("监听到暂停任务,{}", jobKey);
    }

    @Override
    public void jobsPaused(String jobGroup) {
        log.info("监听到暂停任务组,{}", jobGroup);
    }

    @Override
    public void jobResumed(JobKey jobKey) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void jobsResumed(String jobGroup) {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void schedulerError(String msg, SchedulerException cause) {
        log.error("监听到错误任务,{}", msg, cause);
    }

    @Override
    public void schedulerInStandbyMode() {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void schedulerStarted() {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void schedulerStarting() {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void schedulerShutdown() {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void schedulerShuttingdown() {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    @Override
    public void schedulingDataCleared() {
        if (log.isDebugEnabled()) {
            loggerMethodCall();
        }
    }

    /**
     * 获取当前方法, debug 级别日志
     */
    private void loggerMethodCall() {
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        String logText = "监听到任务调度方法被调用,方法名:" + methodName;
        log.info(logText);
    }

}
